/**************************************************************
  > File Name: recShmget.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年02月23日 星期三 03时16分16秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

#define PATHNAME "/tmp/out"

int main(int argc, char **argv)
{
	key_t key;
	int shmid;
	int proc_id = 1;
	size_t size = 4096;
	int *shmp = NULL;

	key = ftok(PATHNAME, proc_id);
	if (key < 0)
	{
		perror("ftok()");
		exit(1);
	}
	
	shmid = shmget(key, size, IPC_CREAT | 0666);
	if (shmid < 0)
	{
		perror("shmget()");
		exit(1);
	}
	
	shmp = shmat(shmid, NULL, 0);
	if (shmp == NULL)
	{
		perror("shmat()");
		exit(1);
	}
	
	printf("%d\n",*shmp);	
	shmdt(shmp);
    exit(0);
}
